package com.example.pc.updata.DaTi;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.pc.http.API;
import com.example.pc.http.Okhttp;
import com.example.pc.updata.AppInfo;
import com.example.pc.updata.Baes.BaseActivity;
import com.example.pc.updata.R;
import com.example.pc.updata.SetIp_Activity;
import com.example.pc.updata.obj.User;
import com.example.pc.updata.obj.login;
import com.example.pc.updata.tools.ActivityTools;
import com.example.pc.utils.JSONUtils;
import com.example.pc.utils.SpfUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class LoginActivity extends BaseActivity {


    @BindView(R.id.et_code)
    EditText etCode;

    @BindView(R.id.et_phone)
    EditText etPhone;


    @BindView(R.id.btn_login)
    Button btnLogin;

    @BindView(R.id.btn_reg)
    Button btn_reg;
    @BindView(R.id.et_yzm)
    EditText etYzm;
    @BindView(R.id.tv_yzm)
    TextView tvYzm;
    String yzm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reister);
        ButterKnife.bind(this);
        ArrayList<String> array = new ArrayList<String>();
        Random rand = new Random();
        for (int i = 0; i < 4; i++) {
            int choice = rand.nextInt(3);
            switch (choice) {
                case 0:
                    int num = rand.nextInt(9);
                    array.add(num + "");
                    break;
                case 1:
                    int num2 = rand.nextInt(25) + 65;
                    char low = (char) num2;
                    array.add(low + "");
                    break;
                case 2:
                    int num3 = rand.nextInt(25) + 97;
                    char high = (char) num3;
                    array.add(high + "");
                    break;
                default:
                    break;
            }
        }

        yzm = array.get(0) + array.get(1) + array.get(2) + array.get(3) + "";
        tvYzm.setText(yzm + "");
    }

    String phone;
    String code;

    @OnClick({R.id.btn_login, R.id.btn_reg, R.id.btn_ip, R.id.btn_gg})
    public void onClick(View v) {
        code = etCode.getText().toString();

        switch (v.getId()) {

            case R.id.btn_login:

                phone = etPhone.getText().toString();
                code = etCode.getText().toString();
                String etYzm2 = etYzm.getText().toString();
                if (phone.equals("")) {
                    showMessage("请输入账户！");
                    return;
                }

                if (code.equals("")) {
                    showMessage("请输入密码！");
                    return;
                }
                if (!String.valueOf(yzm).equals(etYzm2)){
                    showMessage("验证码错误");
                    return;
                }
                loginNet();
                break;
            case R.id.btn_reg:
                ActivityTools.goActivity(LoginActivity.this, RegActivity.class);
                break;

            case R.id.btn_ip:
                ActivityTools.goActivity(LoginActivity.this, SetIp_Activity.class);
                break;

            case R.id.btn_gg:
                ActivityTools.goActivity(LoginActivity.this, GengGaiActivity.class);
                break;


        }
    }


    protected void onDestroy() {
        super.onDestroy();
        //用完回调要注销掉，否则可能会出现内存泄露
    }

    private void loginNet() {
        if (phone.equals("admin") && code.equals("admin")) {
            ActivityTools.goActivity(LoginActivity.this, Main_LS_Activity.class);
            finish();
        } else {
            API.login(phone, code, new Okhttp.Objectcallback() {
                @Override
                public void onsuccess(String st) {
                    Log.e("st", "xx" + st);
                    login login = JSONUtils.parseJSON(st, login.class);

                    if (login.isIssuccess() == true) {
                        String user_account = login.getData().getUser_account();
                        String user_uid = login.getData().getUser_uid();
                        SpfUtils.put(LoginActivity.this, "neme", user_account);
                        SpfUtils.put(LoginActivity.this, "user_uid", user_uid);
                        ActivityTools.goActivity(LoginActivity.this, Main_XS_Activity.class);
                        finish();
                    } else {
                        Toast.makeText(LoginActivity.this, "" + login.getMsg(), Toast.LENGTH_SHORT).show();

                    }

                }

                @Override
                public void onFalia(int code, String errst) {
                    Toast.makeText(LoginActivity.this, "网络错误" + errst, Toast.LENGTH_SHORT).show();

                }
            });

        }
        //  http://localhost:8080/jj/syslogin?username=admin&password=admin


//        BmobQuery<User> categoryBmobQuery = new BmobQuery<>();
//        categoryBmobQuery.addWhereEqualTo("admin", phone);
//        categoryBmobQuery.addWhereEqualTo("password", code);
//        categoryBmobQuery.findObjects(new FindListener<User>() {
//            @Override
//            public void done(List<User> list, BmobException e) {
//                if (e == null) {
//                    if (!list.isEmpty()&&list.size()>0){
//                        AppInfo.user2 = list.get(0);
//
//                        if ( AppInfo.user2.getAdmin().equals("admin")){
//                            ActivityTools.goActivity(LoginActivity.this, IndexActivity.class);
//                        }else{
//                            ActivityTools.goActivity(LoginActivity.this, DaTiActivity.class);
//                        }
//
//                    }else {
//                        showMessage("用户名密码错误");
//                    }
//                } else {
//                    showMessage("用户名密码错误");
//                }
//            }
//        });
    }

}
